package org.example;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Test2 {
    //leetcode 904 水果成篮 https://leetcode.cn/problems/fruit-into-baskets/
    public int totalFruit1(int[] nums) {
        int n = nums.length;
        int slow = 0, fast = 0, ret = 0, kind = 0;
        int[] hash = new int[n];
        while (fast < n) {
            if (hash[nums[fast]]++ == 0) kind++;
            if (kind == 2) {
                ret = Math.max(ret,fast - slow + 1);
            }
            while (kind > 2) {
                int out = nums[slow++];
                if (--hash[out] == 0) kind--;
            }
            fast++;
        }
        if (ret < 2) ret = n;
        return ret;
    }

    public int totalFruit(int[] nums) {
        int n = nums.length;
        int slow = 1, fast = 1, ret = 0, kinds = 1;
        int[] hash = new int[n];
        hash[nums[0]]++;
        while (fast < n) {
            while (fast < n && kinds <= 2) {
                if (hash[nums[fast++]]++ == 0) kinds++;
            }
            ret = Math.max(ret,fast - slow);
            while (kinds > 2) {
                int out = nums[slow++];
                if (--hash[out] == 0) kinds--;
            }
        }
        return ret;
    }

    public static void main(String[] args) {
        Test2 test2 = new Test2();
        int[] arr = {1,2,3,2,2};
        test2.totalFruit(arr);
        System.out.println(666);
    }
}
